package com.enba.mallapi.module.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enba.boot.core.base.Result;
import com.enba.boot.core.context.SecurityContextHolder;
import com.enba.boot.core.utils.TreeNode;
import com.enba.mallapi.annotation.DistributedLock;
import com.enba.mallapi.module.auth.controller.req.MenuAddUpdateReq;
import com.enba.mallapi.module.auth.entity.Menu;
import com.enba.mallapi.module.auth.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 菜单表
 *
 * @author 恩爸编程
 * @since 2025-02-28
 */
@RestController
@RequestMapping("/auth/menu")
@Api(tags = "菜单表接口")
public class MenuController {

  @Resource private IMenuService menuService;

  /**
   * 查询所有接口
   *
   * @return list
   */
  @GetMapping
  public Result<List<Menu>> findAll() {
    return Result.success(menuService.list());
  }

  /**
   * 根据id查询数据接口
   *
   * @param id id
   * @return result
   */
  @GetMapping("/{id}")
  public Result<Menu> findOne(@PathVariable Integer id) {
    return Result.success(menuService.getById(id));
  }

  /**
   * 分页查询接口
   *
   * @param pageNum nume
   * @param pageSize size
   * @return result
   */
  @GetMapping("/page")
  public Result<Page<Menu>> findPage(
      @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
    QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
    return Result.success(menuService.page(new Page<>(pageNum, pageSize), queryWrapper));
  }

  /**
   * 新增和更新接口
   *
   * @param req menu
   * @return result
   */
  @DistributedLock
  @PostMapping
  public Result<Boolean> saveOrUpdate(@RequestBody MenuAddUpdateReq req) {
    return Result.success(menuService.saveOrUpdate(req));
  }

  /**
   * 删除接口
   *
   * @param id id
   * @return result
   */
  @DeleteMapping("/{id}")
  public Result<Boolean> delete(@PathVariable Integer id) {
    return Result.success(menuService.removeById(id));
  }

  /**
   * 批量删除接口
   *
   * @param ids ids
   * @return result
   */
  @PostMapping("/del/batch")
  public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
    return Result.success(menuService.removeByIds(ids));
  }

  // 获取用户菜单列表
  @ApiOperation("获取用户菜单列表（目录和菜单）")
  @GetMapping("/user-menu")
  public Result<List<TreeNode>> getUserMenu() {
    return Result.success(menuService.getUserMenu(SecurityContextHolder.getUserId()));
  }

  @ApiOperation("角色列表-分配权限（反显指定用户拥有的菜单权限）")
  @GetMapping("/all-treeTable-menu/user-perms/{roleId}")
  @DistributedLock
  public Result<List<TreeNode>> getRoleAllMenuIdSet(@PathVariable Long roleId) {
    return Result.success(menuService.getRoleAllMenuIdSet(roleId));
  }

  @ApiOperation("获取所有菜单")
  @GetMapping("/all-menu")
  public Result<List<TreeNode>> getAllMenu() {
    return Result.success(menuService.getAllMenu());
  }

  @ApiOperation("获取所有权限，树形表格结构返回")
  @GetMapping("/all-treeTable-menu")
  public Result<List<TreeNode>> getAllTreeTableMenu() {
    return Result.success(menuService.getAllTreeTableMenu());
  }
}
